package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.OrdersDto;
import com.wizz.trade.information.dto.page.OrdersPageDto;
import com.wizz.trade.information.entity.Orders;
import com.wizz.trade.information.service.OrdersService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.vo.OrdersVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 资料模块/订单模式
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    /**
     * 添加操作
     * @param ordersDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult addOrders(@RequestBody OrdersDto ordersDto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersDto,orders);
        ordersService.addOrders(orders);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新（编辑）操作
     * @param ordersDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult updateOrders(@RequestBody OrdersDto ordersDto, @PathVariable("id") Integer id) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersDto,orders);
        orders.setId(id);
        ordersService.updateOrders(orders);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销）操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult deleteOrders(@PathVariable("id") Integer id) {
        ordersService.deleteOrders(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     * @param ordersPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('基础信息操作')")

    public ResponseResult<PageVo<OrdersVo>> list(OrdersPageDto ordersPageDto){
        PageVo<Orders> rawPage = ordersService.getPage(ordersPageDto);
        List<Orders> data = rawPage.getData();
        List<OrdersVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            OrdersVo  ordersVo = new OrdersVo();
            BeanUtils.copyProperties(data.get(i), ordersVo);
            dataVo.add(ordersVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }

    /**
     * 获取订单模式下拉框
     * @return 选项
     */
    @GetMapping("/options")
    public ResponseResult<List<Map<Integer, String>>> getOptions(){
        return new ResponseResult<>(StatusCode.OK, ordersService.getOptions());
    }
}
